﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;
using Runda.EShop.Common.Utility;

namespace Runda.EShop.SaaS.SysManage
{
    public partial class RoleAdd : BasePage
    {
        public Guid RoleID
        {
            get
            {
                if (ViewState["RoleID"] == null) return Guid.Empty;
                else return new Guid(ViewState["RoleID"].ToString());
            }
            set { ViewState["RoleID"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            treeFunction.Attributes.Add("OnClick", "OnTreeNodeChecked()");
            if (!IsPostBack)
            {
                if (Request.QueryString["RoleID"] != null)
                {
                    RoleID = new Guid(Request.QueryString["RoleID"]);
                    SetFormData();
                }
                BindTree();
            }
        }

        private void SetFormData()
        {
            if (RoleID != Guid.Empty)
            {
                SysRoleEntity entity = null;//
                SystemBll bll = new SystemBll();
                entity = bll.Get<SysRoleEntity>(RoleID);

                txtRoleName.Text = entity.RoleName;
                txtRoleDesc.Text = entity.RoleDesc;
            }
        }

        private void Save()
        {
            SystemBll systemBll = new SystemBll();
            bool IsAdd = true;
            SysRoleEntity entity = null;//
            if (RoleID != Guid.Empty)
            {
                SystemBll bll = new SystemBll();
                entity = bll.Get<SysRoleEntity>(RoleID);
                IsAdd = false;
            }
            else
            {
                entity = new SysRoleEntity();
                RoleID = entity.RoleID;

                entity.CreatedBy = entity.LastModifyBy = base.User.Identity.LoginUser.UserID;
                entity.CreatedOn = entity.LastModifyOn = DateTime.Now;
            }

            entity.RoleName = txtRoleName.Text;
            entity.RoleDesc = txtRoleDesc.Text;
            entity.LastModifyOn = DateTime.Now;
            entity.LastModifyBy = Guid.Empty;

            //权限赋值
            List<SysRoleFunctionRelationEntity> RoleFunList = new List<SysRoleFunctionRelationEntity>();
            SysRoleFunctionRelationEntity RoleFuncEntity = null;
            foreach (TreeNode node in treeFunction.CheckedNodes)
            {
                RoleFuncEntity = new SysRoleFunctionRelationEntity();
                RoleFuncEntity.FuncID = node.Value;
                RoleFuncEntity.RoleID = RoleID;
                RoleFuncEntity.Deleted = false;
                RoleFunList.Add(RoleFuncEntity);
            }

            entity.RoleFunctionRelateList = RoleFunList;
            systemBll.SaveRole(entity);

            if (! IsAdd)
            {
                DoOperationLog(EnumHelper.OperationType.Delete, "修改角色，ID:" + RoleID);
            }
            else
            {
                DoOperationLog(EnumHelper.OperationType.Add, "增加角色，ID:" + RoleID);
            }
        }

        private void BindTree()
        {
            SystemBll bll = new SystemBll();
            DataTable dt = bll.BuildAllFunction(RoleID);

            DataRow[] rows = dt.Select("FFuncID = '' or FFuncID is null");

            foreach (DataRow row in rows)
            {
                TreeNode node = CreateNode(row["FuncName"].ToString(), true, row["FuncID"].ToString());
                node.Checked = row["IsChecked"].ToString() == "1" ? true : false;
                treeFunction.Nodes.Add(node);

                RecursivelyPopulate(dt, node);
            }
        }

        private void RecursivelyPopulate(DataTable dt, TreeNode node)
        {
            DataRow[] rows = dt.Select("FFuncID = '" + node.Value + "'");
            foreach (DataRow row in rows)
            {
                TreeNode TreeNode = CreateNode(row["FuncName"].ToString(), false, row["FuncID"].ToString());
                TreeNode.Checked = row["IsChecked"].ToString() == "1" ? true : false;

                node.ChildNodes.Add(TreeNode);
                RecursivelyPopulate(dt, TreeNode);
            }
        }

        private TreeNode CreateNode(string text, bool expanded, string id)
        {
            TreeNode node;
            node = new TreeNode(text, id);
            node.Expanded = expanded;
            node.SelectAction = TreeNodeSelectAction.None;
            return node;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
            ClientScript.RegisterStartupScript(this.GetType(), "SaveAndClose", "alert('保存成功.');parent.$.ligerDialog.hide();parent.$('#cphMain_cphMain_btnQuery').click();", true);
        }
    }
}